package com.wlyuan.open.scanner.application.task.transport;

import com.wlyuan.open.scanner.assembler.ScannerTransportAssembler;
import com.wlyuan.open.scanner.repository.TransportRepository;
import com.wlyuan.open.scanner.repository.WayBillRepository;
import com.wlyuan.open.scanner.repository.impl.BaseOrderRepository;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.util.StopWatch;

import java.util.concurrent.CountDownLatch;

/**
 * @author lijing
 * @description
 */
@Slf4j
@Data
public class TransportTask implements Runnable {
    private int pageNum;
    private int pageSize;
    private Long tenantId;
    private CountDownLatch countDownLatch;
    private WayBillRepository wayBillRepository;
    private TransportRepository transportRepository;
    private BaseOrderRepository baseOrderRepository;

    @Override
    public void run() {
        if (tenantId == null) {
            all();
        } else {
            tenant();
        }
    }

    public void all() {
        StopWatch watch = new StopWatch();
        watch.start();
        try {
            logger.info("运单数据迁移，页码:{}，开始", pageNum);
            // 分页查询订单数据
            var list = wayBillRepository.list(pageNum);
            list.forEach(data -> {
                try {
                    var ts = transportRepository.parse(data.getUp(), data.getDown());
                    baseOrderRepository.sync(ScannerTransportAssembler.assemble(ts));
                } catch (Exception e) {
                    logger.error("运单同步异常：{},{}", data.getUp(), e.getMessage());
                    e.printStackTrace();

                }
            });
            // 完成1个任务
            if (countDownLatch != null) {
                countDownLatch.countDown();
            }
            watch.stop();
            logger.info("运单数据迁移，页码:{}，结束，耗时:{}", pageNum, watch.getTotalTimeSeconds());
        } catch (Exception e) {
            logger.error("运单数据迁移,页码:{}，运行失败", pageNum, e);
            countDownLatch.countDown();
        }
    }

    public void tenant() {
        StopWatch watch = new StopWatch();
        watch.start();
        try {
            logger.info("租户{}运单数据迁移，页码:{}，开始", tenantId, pageNum);
            // 分页查询订单数据
            var list = wayBillRepository.list(pageNum, tenantId);
            list.forEach(data -> {
                try {
                    var ts = transportRepository.parse(data.getUp(), data.getDown());
                    baseOrderRepository.sync(ScannerTransportAssembler.assemble(ts));
                } catch (Exception e) {
                    logger.error("运单同步异常：{},{}", data.getUp(), e.getMessage());
                    e.printStackTrace();

                }
            });
            // 完成1个任务
            if (countDownLatch != null) {
                countDownLatch.countDown();
            }
            watch.stop();
            logger.info("租户{}运单数据迁移，页码:{}，结束，耗时:{}s", tenantId, pageNum, watch.getTotalTimeSeconds());
        } catch (Exception e) {
            logger.error("租户{}运单数据迁移,页码:{}，运行失败", tenantId, pageNum, e);
            countDownLatch.countDown();
        }
    }
}
